package com.yc.http.tomcat.impl;

import com.yc.http.tomcat.HttpSession;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class HttpSessionImpl implements HttpSession {

    private long creationTime;
    private String id;
    private Map<String,Object> attributeMap = new HashMap<>();
    // 会话容器
    public static Map<String,HttpSession> sessionMap = new HashMap<>();

    public HttpSessionImpl() {
        this.creationTime = System.currentTimeMillis();
        id = UUID.randomUUID().toString();
        id = id.replaceAll("-","");
        // 将会话存入会话容器
        sessionMap.put(id,this);
    }

    @Override
    public long getCreationTime() {
        return creationTime;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public long getLastAccessedTime() {
        // 每次接收到浏览器的请求, 则更新该时间
        return 0;
    }

    @Override
    public int getMaxInactiveInterval() {
        // 返回会话最大有效期,
        return 0;
    }

    @Override
    public void invalidate() {
        // 将会话从容器中移除
        sessionMap.remove(this.id);
    }

    @Override
    public void setAttribute(String name, Object value) {
        attributeMap.put(name,value);
    }

    @Override
    public Object getAttribute(String name) {
        return attributeMap.get(name);
    }

    @Override
    public void removeAttribute(String name) {
        attributeMap.remove(name);
    }
}
